package tree.binarytree;


import org.junit.Test;

/**
 * @author mengzhang6
 */
public class BinaryTreeTest {
    /**
     * 创建一棵二叉树
     * <p>
     * <pre>
     *           A
     *     B          C
     *  D     E            F
     *                  G     H
     * </pre>
     */
    public static void createBinTree(BinaryTree bt) {
        BinaryTree.TreeNode root = new BinaryTree.TreeNode(1, "A");
        bt.setRoot(root);

        BinaryTree.TreeNode nodeB = new BinaryTree.TreeNode(2, "B");
        BinaryTree.TreeNode nodeC = new BinaryTree.TreeNode(3, "C");
        BinaryTree.TreeNode nodeD = new BinaryTree.TreeNode(4, "D");
        BinaryTree.TreeNode nodeE = new BinaryTree.TreeNode(5, "E");
        BinaryTree.TreeNode nodeF = new BinaryTree.TreeNode(6, "F");
        BinaryTree.TreeNode nodeG = new BinaryTree.TreeNode(7, "G");

        BinaryTree.TreeNode nodeH = new BinaryTree.TreeNode(8, "H");
        root.setLeftChild(nodeB);
        root.setRightChild(nodeC);
        root.getLeftChild().setLeftChild(nodeD);
        root.getLeftChild().setRightChild(nodeE);
        root.getRightChild().setRightChild(nodeF);

        root.getRightChild().getRightChild().setLeftChild(nodeH);
        root.getRightChild().getRightChild().setRightChild(nodeG);
    }

    @Test
    public void test1() {
        BinaryTree bt = new BinaryTree();
        createBinTree(bt);

        System.out.println("树的节点数量： " + bt.size());
        System.out.println("树的高度： " + bt.height());

    }

    @Test
    public void test2() {
        BinaryTree bt = new BinaryTree();
        createBinTree(bt);

        //简单遍历
        bt.traverse(bt.getRoot());


    }

    @Test
    public void test3() {
        BinaryTree bt = new BinaryTree();
        createBinTree(bt);

        System.out.println("\n\n*******(前序遍历)[中 前 后]遍历*****************");
        bt.preOrder(bt.getRoot());

        System.out.println("\n\n*******(中序遍历)[前 中 后]遍历*****************");
        bt.inOrder(bt.getRoot());

        System.out.println("\n\n*******(后序遍历)[前 后 中]遍历*****************");
        bt.postOrder(bt.getRoot());

    }

    @Test
    public void test4() {
        BinaryTree bt = new BinaryTree();
        createBinTree(bt);

        /**
         * 有问题 @20190102修复
         */
        System.out.println("\n\n***非递归实现****(前序遍历)[ABDECF]遍历*****************");
        bt.nonRecPreOrder(bt.getRoot());
    }

    @Test
    public void test5() {
        BinaryTree bt = new BinaryTree();
        createBinTree(bt);


        System.out.println("\n\n***非递归实现****(中序遍历)[DBEACF]遍历*****************");
        bt.nonRecInOrder(bt.getRoot());

        System.out.println("\n\n***非递归实现****(后序遍历)[DEBFCA]遍历*****************");
        bt.noRecPostOrder(bt.getRoot());
    }


}
